package org.ikastola

import ikastola.Constants;

import org.springframework.dao.DataIntegrityViolationException
import org.springframework.transaction.annotation.Transactional;
import grails.plugins.springsecurity.Secured


@Secured(['ROLE_ADMIN'])
class CourseController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [courseInstanceList: Course.findAllByDeleted(Constants.NOT_DELETED,params), courseInstanceTotal: Course.countByDeleted(Constants.NOT_DELETED)]
    }

    def create() {
        [courseInstance: new Course(params)]
    }
	
	@Transactional
    def save() {
        def courseInstance = new Course(params)
		courseInstance.setDeleted(Constants.NOT_DELETED)
		courseInstance.setClassCreated(new Date())
        if (!courseInstance.save(flush: true)) {
            render(view: "create", model: [courseInstance: courseInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'course.label', default: 'Course'), courseInstance.id])
        redirect(action: "show", id: courseInstance.id)
    }

    def show(Long id) {
        def courseInstance = Course.get(id)
        if (!courseInstance || Constants.DELETED.equals(courseInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'course.label', default: 'Course'), id])
            redirect(action: "list")
            return
        }

        [courseInstance: courseInstance]
    }

    def edit(Long id) {
        def courseInstance = Course.get(id)
        if (!courseInstance || Constants.DELETED.equals(courseInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'course.label', default: 'Course'), id])
            redirect(action: "list")
            return
        }

        [courseInstance: courseInstance]
    }

	@Transactional
    def update(Long id, Long version) {
        def courseInstance = Course.get(id)
        if (!courseInstance || Constants.DELETED.equals(courseInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'course.label', default: 'Course'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (courseInstance.version > version) {
                courseInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'course.label', default: 'Course')] as Object[],
                          "Another user has updated this Course while you were editing")
                render(view: "edit", model: [courseInstance: courseInstance])
                return
            }
        }

        courseInstance.properties = params
		courseInstance.setClassUpdated(new Date())
		
        if (!courseInstance.save(flush: true)) {
            render(view: "edit", model: [courseInstance: courseInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'course.label', default: 'Course'), courseInstance.id])
        redirect(action: "show", id: courseInstance.id)
    }

	@Transactional
    def delete(Long id) {
        def courseInstance = Course.get(id)
        if (!courseInstance || Constants.DELETED.equals(courseInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'course.label', default: 'Course'), id])
            redirect(action: "list")
            return
        }

		def student = Student.findByCourseAndDeleted(courseInstance,Constants.NOT_DELETED)
		if (student) {
			flash.message = "El curso no puede ser eliminado dado que est&aacute; asignado a un Alumno."
			redirect(action: "list")
			return
		}
		
        try {
            //courseInstance.delete(flush: true)
			courseInstance.setDeleted(1)
			courseInstance.setClassUpdated(new Date())
			courseInstance.save(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'course.label', default: 'Course'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'course.label', default: 'Course'), id])
            redirect(action: "show", id: id)
        }
    }

}
